package com.user.query.utils;

import org.springframework.beans.BeansException;
import org.springframework.beans.factory.BeanInitializationException;
import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
import org.springframework.beans.factory.config.PropertyPlaceholderConfigurer;

import java.util.Properties;


public class EncryptablePropertyPlaceholderConfigurer extends  PropertyPlaceholderConfigurer {
	
	 private static final String key = "0002000200020002"; 
     
	 @SuppressWarnings("static-access")
	protected void processProperties(ConfigurableListableBeanFactory beanFactory, Properties props) throws BeansException { 
          System.out .println("正在解密系统文件..."); 
          try { 
               Des des = new Des();
               // mysql DJ---------------------- 
               //driver
               String mdriver = props.getProperty("jdbc.driver"); 
               if (mdriver != null) { 
                    String mdriverVal = des.Decrypt(mdriver, des.hex2byte(key)); 
                    props.setProperty("jdbc.driver", mdriverVal); 
               } 
               
               //url
               String mUrl = props.getProperty("jdbc.url"); 
               if (mUrl != null) { 
                    String mUrlVal = des.Decrypt(mUrl, des.hex2byte(key)); 
                    props.setProperty("jdbc.url", mUrlVal); 
               } 
               
               //jdbc.user
               String mUser = props.getProperty("jdbc.user"); 
               if (mUser != null) { 
                    String mUserVal = des.Decrypt(mUser, des.hex2byte(key)); 
                    props.setProperty("jdbc.user", mUserVal); 
               } 
               
               //jdbc.password
               String mPassword = props.getProperty("jdbc.password"); 
               if (mPassword != null) { 
                    String passwordVal = des.Decrypt(mPassword, des.hex2byte(key)); 
                    props.setProperty("jdbc.password", passwordVal); 
               } 
                
               // oracle DJ---------------------- 
               //jdbc.driver2
               String  driver2= props.getProperty("jdbc.driver2"); 
               if (driver2 != null) { 
                    String driver2Val = des.Decrypt(driver2, des.hex2byte(key)); 
                    props.setProperty("jdbc.driver2", driver2Val); 
               } 
               
               //jdbc.url2
               String url2 = props.getProperty("jdbc.url2"); 
               if (url2 != null) { 
                    String url2Val = des.Decrypt(url2,des.hex2byte(key)); 
                    props.setProperty("jdbc.url2", url2Val); 
               } 
               
               //jdbc.user2
               String user2 = props.getProperty("jdbc.user2"); 
               if (user2 != null) { 
                    String user2Val = des.Decrypt(user2, des.hex2byte(key)); 
                    props.setProperty("jdbc.user2", user2Val); 
               } 
               
               //jdbc.password2
               String password2 = props.getProperty("jdbc.password2"); 
               if (password2 != null) { 
                    String password2Val = des.Decrypt(password2, des.hex2byte(key)); 
                    props.setProperty("jdbc.password2", password2Val);
               } 
               
               super.processProperties(beanFactory, props); 
          } catch (Exception e) { 
               e.printStackTrace(); 
               throw new BeanInitializationException(e.getMessage()); 
          } 
     } 
            
}
